/**
****************************************************************************************
 * @                       _oo0oo_
 * @                      o8888888o
 * @                      88" . "88
 * @                      (| -_- |)
 * @                      0\  =  /0
 * @                    ___/`---'\___
 * @                  .' \\|     |// '.
 * @                 / \\|||  :  |||// \
 * @                / _||||| -:- |||||- \
 * @               |   | \\\  - /// |   |
 * @               | \_|  ''\---/''  |_/ |
 * @               \  .-\__  '-'  ___/-. /
 * @             ___'. .'  /--.--\  `. .'___
 * @          ."" '<  `.___\_<|>_/___.' >' "".
 * @         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * @         \  \ `_.   \_ __\ /__ _/   .-` /  /
 * @     =====`-.____`.___ \_____/___.-`___.-'=====
 * @                       `=---='
 * @
 * @
 * @     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * @
 * @           佛祖保佑     永不宕机     永无BUG
****************************************************************************************
*/


/**
****************************************************************************************
 * @Author: hekies fortwater969@163.com
 * @Version: 
 * @Date: 2023-04-17 09:22:44
 * @LastEditors: hekies fortwater969@163.com
 * @LastEditTime: 2023-04-24 15:32:39
 * @FilePath: \FOC_F103C8T6\USER\main.c
 * @Description: 
 * @Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
****************************************************************************************
*/

#include "stm32f10x.h"
#include "config.h"
#include "bsp_uart.h"
#include "bsp_adc.h"
#include "bsp_delay.h"
#include "app_timer.h"
#include "app_log.h"

#include "app_log.h"
#include "elog.h"

#include "app_key.h"

#if MULTIBUTTON_ENABLE
#include "multi_button.h"
#endif

#if MW8266D_ENABLE
#include "atk_mw8266d.h"
#endif

#if MPU6050_ENABLE
#include "app_mpu6050.h"
#endif

#include "smarttimer.h"

#include "i2c_hw.h"
#include "spi_hw.h"

#include "app_oled.h"
//#include "bsp_oled.h"
#include "IQS7211E.h"

#if MOTOR_TB6612_ENABLE
#include "app_motor.h"
#endif

#include "ws2812b.h"

#include <stdio.h>
#include <stdlib.h>

#if MAIN_LOG_SWITCH
#define LOG_MAIN(fmt, args...)   DBG_PRINTF(fmt, ##args)
#else
#define LOG_MAIN(fmt, args...)
#endif

#define WIFI_SSID "LAPTOP-A4LAV39L_2817"
#define WIFI_PWD "3L900+2k"

#if MULTIBUTTON_ENABLE
struct Button btn1;
struct Button btn2;
struct Button btn3;
#endif

struct date{
    uint16_t year;
    uint8_t  month;
    uint8_t  day;
    uint8_t hour;
    uint8_t minute;
    uint8_t second;
};
struct date m_date;

const char datestr[] = "2023:04:06#17:16:00";

struct i2c_dev_device i2c0;
struct i2c_dev_device i2c1;
#if MPU6050_ENABLE
struct i2c_dev_device i2c2;
u8 motor_start_flag = 0;
#endif
struct spi_bus_device spi_bus1;

int main(void)
{
    stim_init();
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

#if ENABLE_USART1
    Uart1_Init(115200);
#endif

    Easylogger_Init();

#if MW8266D_ENABLE
    if(mw8266d_init(115200) == MW8266D_EOK)
    {
        log_d("where\r\n");
    }
#endif

    app_key_init();
    ws2812_Init();
#if MULTIBUTTON_ENABLE
    button_init(&btn1, read_button_GPIO, 0, KEY1);
    button_init(&btn2, read_button_GPIO, 0, KEY2);
    //button_init(&btn3, read_button_GPIO, 0, KEY3);
    app_key_process();
    button_start(&btn1);
    button_start(&btn2);
    //button_start(&btn3);
    stim_loop(5, button_ticks, STIM_LOOP_FOREVER);
#endif

#if IQS7211E_ENABLE
    tmp1 = IQS7211E_Init(&i2c1);
    if(tmp1 == 0)
    {
        log_e("This is normal IQS8211E i2c!");
    }
#endif

#if MPU6050_ENABLE
    stm32f1xx_i2c_init(&i2c2);
    tok_mpu6050_init();
#endif

    log_a("This is assert log output!");
    log_e("This is error log output!");
    log_w("This is warning log output!");
    log_i("This is info log output!");
    log_d("This is debug log output!");
    log_v("This is verbose log output!");
    
    stim_loop(5, fix_horse_race_lamp, STIM_LOOP_FOREVER);
    usart_send_string(USART1, "this foc_c8t6\r\n");
    
    while(1)
    {
        // horse_race_lamp(200); //跑马灯
        stim_mainloop();
    }
}

